# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free 
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
#    MA  02110-1301  USA

import msgWin, button, msg
import time, string
import gettext
_ = gettext.gettext

def handleOption(ui, i):
	if i <> -2:
		ui.options_win = 0		
		if i <> -1:
			ui.options_index = -1
			ui.options_list = 0
		if i == 0:
			# Log
			if ui.winOpt2.passport == ui.winOpt2.nick:
				ui.winOpt2.nick = ui.winOpt2.realnick
			ctime = time.strftime(ui.mam.config['timestamp_format'], time.localtime())
			ui.printMsg('\n%s ' % ctime, ui.COLORS.WHITE)
			if ui.winOpt2.nick <> '':
				str = ui.winOpt2.nick + ' (' + ui.winOpt2.passport + ')'
			else:
				str = ui.winOpt2.passport

			# Block the user and log in events
			show_groups = int(ui.mam.config['show_groups'])
			if ui.winOpt2.blocked:
				ui.mam.m.userunblock(ui.winOpt2.passport)
				ui.printMsg('%s %s %s' % (_('User'), str, _('has been unblocked')), ui.COLORS.MSG_UNBLOCK)
				if show_groups:
					num = ui.mam.contactListManager.gList.grupos[ui.mam.contactListManager.default_group].passports.passinList(ui.winOpt2.passport)
					ui.printMsg(num)
					ui.refreshScr()
					ui.mam.contactListManager.gList.grupos[ui.mam.contactListManager.default_group].passports.passports[num].blocked = 0
				else:
					num = ui.mam.contactListManager.pList.passports.passinList(ui.winOpt2.passport)
					ui.mam.contactListManager.pList.passports[num].blocked = 0
			else:
				ui.mam.m.userblock(ui.winOpt2.passport)
				ui.printMsg('%s %s %s' % (_('User'), str, _('has been blocked')), ui.COLORS.MSG_BLOCK)
				if show_groups:
					num = ui.mam.contactListManager.gList.grupos[ui.mam.contactListManager.default_group].passports.passinList(ui.winOpt2.passport)
					ui.mam.contactListManager.gList.grupos[ui.mam.contactListManager.default_group].passports.passports[num].blocked = 1
				else:
					num = ui.mam.contactListManager.pList.passinList(ui.winOpt2.passport)
					ui.mam.contactListManager.pList.passports[num].blocked = 1

			# Rebuildentire_list so we can see the [!]
			relative_pos = ui.scrList.default_scrListpos%(ui.maxY-11)
			ui.scrList.build_entire_scrList(ui.scrList.default_scrListpos - relative_pos)
			ui.scrList.build_visible_scrList()
			ui.scrList.print_scrList()

		del ui.winOpt2
		ui.refreshScr()

# We create the MsgWin
def showWin(mam):
	#if mam.contactListManager.default_user == -1:
	#	return
	# Get user data
	show_groups = int(mam.config['show_groups'])
	if show_groups:
		passport = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].name
		nick = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].nick
		realnick = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].realnick
		blocked = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].blocked
	else:
		passport = mam.contactListManager.pList.passports[mam.contactListManager.default_user].name
		nick = mam.contactListManager.pList.passports[mam.contactListManager.default_user].nick
		realnick = mam.contactListManager.pList.passports[mam.contactListManager.default_user].realnick
		blocked = mam.contactListManager.pList.passports[mam.contactListManager.default_user].blocked
	if blocked:
		str = _('UNBLOCK CONTACT')
		bl = _("unblock")
	else:
		str = _('BLOCK CONTACT')
		bl = _("block")
	# Create win
	msgWin.MsgWin(mam, str, \
	[msg.Msg(_('Are you sure you want to %s the contact') % bl, '', 1), \
	msg.Msg(nick, mam.ui.COLORS.OPTIONS_WIN4, 1), \
	msg.Msg("(%s)" % passport, mam.ui.COLORS.OPTIONS_WIN4, 0), \
	msg.Msg(" ?", '', 1)], \
	[button.Button(_("YES"), 1), button.Button(_("NO"), 0)], mam.ui.maxX * 5 / 7)
	mam.ui.winOpt2.passport = passport
	mam.ui.winOpt2.nick = nick
	mam.ui.winOpt2.blocked = blocked
	mam.ui.winOpt2.realnick = realnick
